package com.cs4261.smartalarm.frontend;

import java.util.List;

import com.cs4261.smartalarm.R;
import com.cs4261.smartalarm.backend.Alarm;
import com.cs4261.smartalarm.backend.AlarmReceiver;
import com.cs4261.smartalarm.backend.AlarmsDatabaseHelper;
import com.cs4261.smartalarm.component.AlarmAdapter;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

/**
 * @author Joshua
 *
 */
public class ManageAlarmsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_manage_alarms);
		
		buildAlarmList();
	}

	/**
	 * 
	 */
	private void buildAlarmList() {
		AlarmsDatabaseHelper alarmDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alarmDBHelper.open();
		List<Alarm>  alarmList = alarmDBHelper.getAllAlarms();
		alarmDBHelper.close();
				
		ListView myListView = (ListView) findViewById(R.id.listView1);
		AlarmAdapter customAdapter;
		
		if(alarmList.size() > 0) {
			customAdapter = new AlarmAdapter(this, R.layout.alarm_entity, alarmList);
		}
		else {
			customAdapter = new AlarmAdapter(this, R.layout.alarm_entity);
		}
		
		myListView.setAdapter(customAdapter);
	}
	
	/**
	 * @param view
	 */
	public void onNewAlarm(View view) {
		//Start a new Intent
		Intent intent = new Intent(ManageAlarmsActivity.this, CreateAlarmActivity.class);
	    startActivity(intent);
	}
	
	/**
	 * @param view
	 */
	public void onToggleAlarm(View view) {
		AlarmsDatabaseHelper alDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alDBHelper.open();
		Alarm alarm = alDBHelper.getAlarmById(Long.parseLong(((TextView)((View) view.getParent().getParent().getParent()).findViewById(R.id.alarm_id)).getText().toString()));
		if(alarm.getActive() == 0) {
			alarm.setActive(1);
		}
		else {
			alarm.setActive(0);
		}
		alDBHelper.updateAlarm(alarm);		
		alDBHelper.close();
	}
	
	/**
	 * @param view
	 */
	public void onToggleDetails(View view) {
		 View detail_layout = ((View) view.getParent()).findViewById(R.id.detail_layout);
		 
		 if(detail_layout.getVisibility() == View.GONE) {
			 detail_layout.setVisibility(View.VISIBLE);
		 }
		 else {
			 detail_layout.setVisibility(View.GONE);
		 }
	}
	
	/**
	 * @param view
	 */
	public void onEditAlarm(View view) {
		Intent intent = new Intent(ManageAlarmsActivity.this, EditAlarmActivity.class);
		intent.putExtra("alarm_id", ((TextView) ((View) view.getParent().getParent().getParent().getParent()).findViewById(R.id.alarm_id)).getText().toString());
	    startActivity(intent);	  
	}
	
	/**
	 * @param view
	 */
	public void onDeleteAlarm(View view) {
		TextView alarm_id = (TextView) ((View) view.getParent().getParent().getParent().getParent()).findViewById(R.id.alarm_id);
		String id = alarm_id.getText().toString();
		
		AlarmsDatabaseHelper alarmDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alarmDBHelper.open();
		alarmDBHelper.deleteAlarm(Long.parseLong(id));
		alarmDBHelper.close();
		
		buildAlarmList();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_manage_alarms, menu);
		return true;
	}
}
